package com.example.leero.graduation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.file.FileResource;
import com.example.leero.graduation.API;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.GsonUtil;
import com.example.leero.graduation.Util.ImageLoader;
import com.example.leero.graduation.Util.StringUtil;
import com.example.leero.graduation.model.UserDetail;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * Created by Leero on 2017-4-25.
 */

public class FriendActivity extends AppCompatActivity {
    private ImageButton back;
    private ImageView iv_avatar, iv_frd_sex;
    private TextView tv_username, tv_signature, tv_friend_detail_release_count, tv_friend_detail_join_count,
            tv_sport_num, tv_sport, tv_emotion, tv_college;

    private UserDetail userDetail;
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_dynamic);

        Intent intent = getIntent();
        userId = intent.getIntExtra("id", 1);

        initView();
        getData();

        back = (ImageButton) findViewById(R.id.iv_left);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        iv_avatar = (ImageView) findViewById(R.id.iv_avatar);
        iv_frd_sex = (ImageView) findViewById(R.id.iv_frd_sex);
        tv_username = (TextView) findViewById(R.id.tv_username);
        tv_signature = (TextView) findViewById(R.id.tv_signature);
        tv_friend_detail_release_count = (TextView) findViewById(R.id.tv_friend_detail_release_count);
        tv_friend_detail_join_count = (TextView) findViewById(R.id.tv_friend_detail_join_count);
        tv_sport_num = (TextView) findViewById(R.id.tv_sport_num);
        tv_sport = (TextView) findViewById(R.id.tv_sport);
        tv_emotion = (TextView) findViewById(R.id.tv_emotion);
        tv_college = (TextView) findViewById(R.id.tv_college);

        findViewById(R.id.rl_release_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FriendActivity.this, UserPublishActivity.class);
                intent.putExtra("user_id", userId);
                startActivity(intent);
            }
        });
        findViewById(R.id.rl_join_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FriendActivity.this, UserJoinActivity.class);
                intent.putExtra("user_id", userId);
                startActivity(intent);
            }
        });
    }

    private void handleUser() {
        if (!StringUtil.isEmpty(userDetail.avatar)) {
            ImageLoader.load(this, iv_avatar, userDetail.avatar);
        } else {
            iv_avatar.setImageResource(R.drawable.default_avatar_cricle);
        }

        if (userDetail.sex == 0) {
            iv_frd_sex.setImageResource(R.drawable.draw_women);
        } else {
            iv_frd_sex.setImageResource(R.drawable.draw_man);
        }

        tv_username.setText(userDetail.user_name);
        tv_signature.setText(userDetail.per_signature);
        tv_friend_detail_release_count.setText("发布("+ userDetail.publish_count + ")");
        tv_friend_detail_join_count.setText("参与("+ userDetail.join_count + ")");

        String[] sports = userDetail.favorite_sport.split(",");
        int sport_num = sports[0].equals("0") ? 0 : sports.length;
        tv_sport_num.setText("(" + sport_num + ")");
        tv_sport.setText(getSportName(sports));
        tv_emotion.setText(getEmotionString(userDetail.affective_state));
        tv_college.setText(userDetail.college);
    }

    private String getSportName(String[] sports) {
        String result = "";
        for (String sport:sports) {
            switch (sport) {
                case "1":
                    result += "跑步、"; break;
                case "2":
                    result += "羽毛球、"; break;
                case "3":
                    result += "乒乓球、"; break;
                case "4":
                    result += "篮球、"; break;
                case "5":
                    result += "足球、"; break;
                case "6":
                    result += "网球、"; break;
                default: break;
            }
        }
        return  !StringUtil.isEmpty(result) ? result.substring(0, result.length() - 1) : result;
    }

    private String getEmotionString(int state) {
        String result = "";
        switch (state) {
            case 1:
                result = "单身"; break;
            case 2:
                result = "恋爱中"; break;
            case 3:
                result = "保密"; break;
            case 4:
                result = "同性"; break;
        }
        return result;
    }

    private void getData() {
        OkHttpUtils.post().url(API.GET_USER_INFO)
                .addParams("user_id", userId + "")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                userDetail = GsonUtil.fromJson(response, UserDetail.class);
                handleUser();
            }
        });
    }
}
